package com.design.graduation.asdemo.activity;

import android.content.Intent;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.design.graduation.asdemo.R;
import com.design.graduation.asdemo.base.ActionBarActivity;
import com.design.graduation.asdemo.event.BusProvider;
import com.design.graduation.asdemo.event.SolicitingItem;
import com.design.graduation.asdemo.model.ConditionType;
import com.design.graduation.asdemo.model.SolicitingDetails;
import com.design.graduation.asdemo.utils.ResourceUtils;
import com.design.graduation.asdemo.view.ListViewForScrollView;
import com.design.graduation.asdemo.view.ResultAdapter;
import com.squareup.otto.Subscribe;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lijinyuan on 16/2/18.
 */
@EActivity(R.layout.activity_soliciting)
public class SolicitingActivity extends ActionBarActivity implements SeekBar.OnSeekBarChangeListener {

    public int priceProgress;
    private int selectedIndexOfTab;
    private Fragment mContent, priceFragment, distanceFragment;
    private List<SolicitingDetails> mlist;
    private ResultAdapter resultAdapter;
    //    private ConditionType conditionType;
//    private List<ConditionType> list;
    private Boolean showConditions = false;

    @ViewById(R.id.content)
    ListViewForScrollView listViewForScrollView;

    @ViewById(R.id.act_soliciting_price_seekbar)
    SeekBar mSeekBar;
    @ViewById(R.id.act_soliciting_price_number)
    TextView priceNumber;
    @ViewById(R.id.act_soliciting_conditions_bar)
    RelativeLayout conditionsBar;
    @ViewById(R.id.act_soliciting_conditions_more)
    RecyclerView conditionsMore;
    @ViewById(R.id.act_soliciting_more_condition_img)
    ImageView arrowImg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initNormalActionBar();
        setTitle("筛选");
        setLeftMenuIcon(R.drawable.ic_chevron);
        setTheme(R.style.AppTheme);
        BusProvider.register(this);

        selectedIndexOfTab = 0;


        setRightMenuText("开始寻找");

    }


    @AfterViews
    public void initView() {
        mlist = new ArrayList<>();
        mSeekBar.setOnSeekBarChangeListener(this);


    }

    public void showDetailed(View view) {
        if (showConditions) {
            arrowImg.setImageResource(R.drawable.icon_drop_down_arrow);
            RotateAnimation rotateAnimation = new RotateAnimation(0, -90,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            rotateAnimation.setDuration(500);
            rotateAnimation.setFillAfter(true);
            arrowImg.startAnimation(rotateAnimation);
            conditionsMore.setVisibility(View.GONE);
            showConditions = false;
        } else {
            arrowImg.setImageResource(R.drawable.icon_right_arrow);
            RotateAnimation rotateAnimation = new RotateAnimation(0, 90,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            rotateAnimation.setDuration(500);
            rotateAnimation.setFillAfter(true);
            arrowImg.startAnimation(rotateAnimation);
            conditionsMore.setVisibility(View.VISIBLE);
            showConditions = true;

        }


    }


    //底部筛选
    public void onTabSelected(View view) {
        ViewGroup viewGroup = (ViewGroup) view.getParent();
        int indexOfTab = viewGroup.indexOfChild(view);
        changeFragmentByIndexOfTab(indexOfTab);

    }

    private void changeFragmentByIndexOfTab(int indexOfTab) {
        if (indexOfTab == selectedIndexOfTab) return;
        changeTabState(indexOfTab);

        selectedIndexOfTab = indexOfTab;


    }

    private void changeTabState(int indexOfTab) {
        LinearLayout tabLayout = (LinearLayout) this.findViewById(R.id.tab_layout);
        LinearLayout newSelected = (LinearLayout) tabLayout.getChildAt(indexOfTab);
        LinearLayout selectedTab = (LinearLayout) (tabLayout.getChildAt(selectedIndexOfTab));
        selectedTab.setBackgroundColor(ResourceUtils.getColor(this, R.color.white));
        newSelected.setBackgroundColor(ResourceUtils.getColor(this, R.color.black));
    }


    @Click(R.id.act_soliciting_position_re)
    public void toLocation() {
        startActivity(new Intent(SolicitingActivity.this, LocationActivity.class));
    }

    @AfterViews
    public void initViews() {
        mlist = new ArrayList<>();
        mlist.add(new SolicitingDetails("111111", "aaaaa", "11", R.drawable.ic_launcher));
        mlist.add(new SolicitingDetails("222222", "bbbbb", "22", R.drawable.ic_launcher));
        mlist.add(new SolicitingDetails("333333", "ccccc", "33", R.drawable.ic_launcher));
        mlist.add(new SolicitingDetails("444444", "dddddd", "44", R.drawable.ic_launcher));


        resultAdapter = new ResultAdapter(this, mlist, R.layout.item_details);
        listViewForScrollView.setAdapter(resultAdapter);

        listViewForScrollView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(SolicitingActivity.this,CheckDetailsActivity.class));
            }
        });
    }


    @Override
    public void onLeftMenuClicked(View view) {
        super.onLeftMenuClicked(view);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BusProvider.unregister(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        priceProgress = progress;
        priceNumber.setText("0-" + progress + "元");
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }


    @Override
    public void onRightMenuClicked(View view) {
        super.onRightMenuClicked(view);
    }


}

